<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '600px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'default')
      this.setOptions(this.chartData)
    },
    setOptions({ actualData1, actualData2, actualData3, actualAxisX, legendData } = {}) {
      this.chart.setOption({
        title: {
          text: '整体项目预览'

        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}'
        },
        legend: {
          show: true,
          data: legendData
        },
        angleAxis: {
          type: 'category',
          data: actualAxisX
        },
        radiusAxis: {
        },
        polar: {
        },
        series: [
          {
            type: 'bar',
            data: actualData1,
            coordinateSystem: 'polar',
            name: legendData[0],
            stack: 'a',
            emphasis: {
              focus: 'series'
            }
          },
          {
            type: 'bar',
            data: actualData2,
            coordinateSystem: 'polar',
            name: legendData[1],
            stack: 'b',
            emphasis: {
              focus: 'series'
            }
          },
          {
            type: 'bar',
            data: actualData3,
            coordinateSystem: 'polar',
            name: legendData[2],
            stack: 'c',
            emphasis: {
              focus: 'series'
            }
          }
        ]
      })
    }
  }
}
</script>
